const express = require('express');
const router = express.Router();
const Blog = require('../../mongoModuels/rear/blog');
const jwt = require('jsonwebtoken');
const time  = require('./../../utils/time');

router
.get('/getUser', (req, res) => {
    Blog.login.find({}, (err, data) => {
        if(err) {
            res.json({
                status: 0,
                message: '操作失败'
            })
        } else {
            res.json({
                status: 1,
                message: '操作成功',
                data
            })
        }
    })
})

// 查询user

.post('/modUser', (req, res) => {
    if(req.body.id && req.body.userName && req.body.roleId  && req.body.passWord) {
        let token = req.get("Authorization");
        let founderName = jwt.decode(token).username;
        let creationTime = time(new Date().getTime());
        let rows = {
            'username': req.body.userName,
            'roleId': req.body.roleId,
            'founder': founderName,
            'creationTime': creationTime,
            'password': req.body.passWord
        }
        Blog.role.findOne({'_id': rows.roleId}, (err, data) => {
            if(err) {
                res.json({
                    status: 0,
                    message: '修改失败'
                })
            } else {
                rows.role = data.name;
                Blog.login.updateOne({'_id': req.body.id}, rows, err => {
                    if(err) {
                        res.json({
                            status: 0,
                            message: '修改失败'
                        })
                    } else {
                        res.json({
                            status: 1,
                            message: '修改成功'
                        })
                    }
                })
            }
        })
    } else {
        res.json({
            status: 0,
            message: '参数不足'
        })
    }
})
//修改

.post('/addUser', (req, res) => {
    if(req.body.userName && req.body.roleId && req.body.passWord) {
        let token = req.get("Authorization");
        let founderName = jwt.decode(token).username;
        let creationTime = time(new Date().getTime());
        let rows = {
            'username': req.body.userName,
            'roleId': req.body.roleId,
            'founder': founderName,
            'creationTime': creationTime,
            'password': req.body.passWord
        };
        Blog.role.findOne({'_id': rows.roleId}, (err, data) => {
            if(err) {
                res.json({
                    status: 0,
                    message: '添加失败'
                })
            } else {
                rows.role = data.name;
                Blog.login.create(rows, err => {
                    if(err) {
                        res.json({
                            status: 0,
                            message: '添加失败'
                        })
                    } else {
                        res.json({
                            status: 1,
                            message: '添加成功'
                        })
                    }
                })
            }
        })
    } else {
        res.json({
            status: 0,
            message: '参数不足'
        })
    }
})
//增加

.post('/delUser', (req, res) => {
    if(req.body.id) {
        Blog.login.remove({'_id': req.body.id}, err => {
            if(err) {
                res.json({
                    status: 0,
                    message: '删除失败'
                })
            } else{
                res.json({
                    status: 1,
                    message: '删除成功'
                })
            }
        })
    } else {
        res.json({
            status: 0,
            message: '参数不足'
        })
    }
})
//删除

module.exports = router;